﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NHibernate.Criterion;
using ZDSoft.UPMS.Model.BusinessPart;
using ZDSoft.UPMS.Service.BusinessPart;
using ZDSoft.UPMS.Core;
using ZDSoft.UPMS.Web.App;
using ZDSoft.UPMS.Model.SystemPart;
namespace ZDSoft.UPMS.Web.Controllers
{
    public class CourseController : Controller
    {
        //
        // GET: /Course/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Create()
        {
            return View();
        }
        /// <summary>
        /// 得到教师所教课程的信息
        /// </summary>
        /// <returns></returns>
        //public IList<TeacherTeachingCourse>GetTeacherCourser()
        //{
        //    IList<TeacherTeachingCourse> list = new List<TeacherTeachingCourse>();
         
           
             
        //}


        private IList<ICriterion> GetCourse(string CourseID= "")
        {
            List<ICriterion> queryConditions = new List<ICriterion>();
            if (!string.IsNullOrEmpty(CourseID))
            {
                queryConditions.Add(new LikeExpression("ID", CourseID));
            }
            return queryConditions;
        }
        public JsonResult GetAllByID(int pageIndex, int pageSize, string strSearchProperty,
           string strSearchCondition, string strSortProperty, string strSortCondition)
        {
            string  course = Request.QueryString["CourseID"].ToString();
            IList<ICriterion> queryConditions = GetCourse(course);
            IList<Order> orderList = new List<Order>();

            if (strSortProperty != "")
            {
                string[] sortProperties = strSortProperty.Split(',');
                string[] sortConditions = strSortCondition.Split(',');
                orderList = new List<Order>();
                for (int i = 0; i < sortProperties.Length; i++)
                {
                    if (sortConditions[i] == "asc")
                    {
                        orderList.Add(new Order(sortProperties[i], true));
                    }
                    else
                    {
                        orderList.Add(new Order(sortProperties[i], false));
                    }
                }
            }

            int count;
            int pageNum;

            IList<Course> list = new List<Course>();
            IList<Course> Courses = Core.Container.Instance.Resolve<ICourseService>().GetPaged(queryConditions, orderList, pageIndex, pageSize, out count);
            int tmpcountI = count / pageSize;
            float temCountF = float.Parse(count.ToString()) / float.Parse(pageSize.ToString());
            if (temCountF > float.Parse(tmpcountI.ToString()))
            {
                pageNum = tmpcountI + 1;
            }
            else
            {
                pageNum = tmpcountI;
            }

            foreach (Course cou in Courses)
            {
                JsonHelper.ToJaonResult<Course>(cou);
                list.Add(cou);

            }
            list.Add(new Course() { ID = pageNum });
            JsonResult result = this.Json(list, JsonRequestBehavior.AllowGet);
            return result;
        }

        /// <summary>
        /// 得到course表的内容
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="strSearchProperty"></param>
        /// <param name="strSearchCondition"></param>
        /// <param name="strSortProperty"></param>
        /// <param name="strSortCondition"></param>
        /// <returns></returns>

        public JsonResult GetPaged(int pageIndex, int pageSize, string strSearchProperty,
         string strSearchCondition, string strSortProperty, string strSortCondition)
        {
            IList<ICriterion> queryConditions = new List<ICriterion>();
            IList<Order> orderList = new List<Order>();
            if (strSearchProperty != "")
            {
                string[] searchProperties = strSearchProperty.Split(',');
                string[] searchConditions = strSearchCondition.Split(',');
                queryConditions = new List<ICriterion>();
                for (int i = 0; i < searchProperties.Length; i++)
                {
                    queryConditions.Add(Expression.Eq(searchProperties[i], searchConditions[i]));
                }
            }
            if (strSortProperty != "")
            {
                string[] sortProperties = strSortProperty.Split(',');
                string[] sortConditions = strSortCondition.Split(',');
                orderList = new List<Order>();
                for (int i = 0; i < sortProperties.Length; i++)
                {
                    if (sortConditions[i] == "asc")
                    {
                        orderList.Add(new Order(sortProperties[i], true));
                    }
                    else
                    {
                        orderList.Add(new Order(sortProperties[i], false));
                    }
                }
            }

            int count;
            int pageNum;

            IList<Course> list = new List<Course>();
            IList<Course> Courses = ZDSoft.UPMS.Core.Container.Instance.Resolve<ICourseService>().GetPaged(queryConditions, orderList, pageIndex, pageSize, out count);

            int tmpcountI = count / pageSize;
            float temCountF = float.Parse(count.ToString()) / float.Parse(pageSize.ToString());
            if (temCountF > float.Parse(tmpcountI.ToString()))
            {
                pageNum = tmpcountI + 1;
            }
            else
            {
                pageNum = tmpcountI;
            }

            foreach (Course course in Courses)
            {
                JsonHelper.ToJaonResult<Course>(course);
                list.Add(course);

            }
            list.Add(new Course() { ID = pageNum });
            JsonResult result = this.Json(list, JsonRequestBehavior.AllowGet);
            return result;
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="course"></param>
        [HttpPost]
        public void Create(Course course)
        {
            ZDSoft.UPMS.Core.Container.Instance.Resolve<ICourseService>().Create(course);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ActionResult Update(int Id)
        {
            Course course = ZDSoft.UPMS.Core.Container.Instance.Resolve<ICourseService>().FindByID(Id);
            return View(course);
        }
        [HttpPost]
        public void Update(Course course)
        {

            Course model = ZDSoft.UPMS.Core.Container.Instance.Resolve<ICourseService>().FindByID(course.ID);
            ModelUtilityHelper.Transfer(course, model);
            ZDSoft.UPMS.Core.Container.Instance.Resolve<ICourseService>().Update(model);
        }
        /// <summary>
        /// 改变状态
        /// </summary>
        /// <param name="id"></param>
        [HttpPost]
        public void UpdateIsActive(string id)
        {
            Course modl = ZDSoft.UPMS.Core.Container.Instance.Resolve<ICourseService>().FindByID(int.Parse(id));
            if (modl.IsActive == true)
            {
                modl.IsActive = false;
            }
            else
            {
                modl.IsActive = true;
            }
            ZDSoft.UPMS.Core.Container.Instance.Resolve<ICourseService>().Update(modl);

        }
        /// <summary>
        /// 根据id得到course表的内容
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public  string GetById(string id)
        {
            Course entity = ZDSoft.UPMS.Core.Container.Instance.Resolve<ICourseService>().FindByID(int.Parse(id));
            IList<KeyValuePair<string, InvokeMethod>> list = new List<KeyValuePair<string, InvokeMethod>>();
            string str = entity.ToJsonString<Course>(list);
            return str;
        }
    }
}
